import { Component, HostListener, OnDestroy, OnInit } from "@angular/core";
import { sjgzxinModuledatas } from "../pubcomponent/pageTitle.datas";
import { LocalStorageService } from "../local-storage-service";
import { CommService } from "../comm-service";
import { MyhttpService } from "../myhttp.service";
import { FormBuilder, FormGroup } from "@angular/forms";
import { MyValidator } from "../myvalidator-util";

declare var $: any;

@Component({
  template: `
  <div class="all_bg paddingTop">
  <div class="">
  <div class="marginBottom">
  <ng-container *ngIf="actionobj1.length">
    <portlet-action class="actions" [o]="actionobj1" (outhander)="actionHander($event)"></portlet-action>
  </ng-container>
  </div>
      <div class="portlet-body">
          <div class=" row">
              <form class="form-horizontal" role="form">
                  <div class="col-lg-2">
                      <div class="form-group">
                          <label class="control-label col-lg-5">试卷包名称：</label>
                          <div class="col-lg-7">
                              <input type="text" class="form-control" name="sjbmc" [(ngModel)]="s_obj.sjbmc" />
                          </div>
                      </div>
                  </div>
                  <div class="col-lg-3">
                      <div class="form-group">
                          <label class="control-label col-lg-5">目的考区名称:</label>
                          <div class="col-lg-7">
                              <input type="text" class="form-control" name="mdkqmc" [(ngModel)]="s_obj.mdkqmc" />
                          </div>
                      </div>
                  </div>
                  <div class="col-lg-3">
                      <div class="form-group">
                          <label class="control-label col-lg-5 ">目的考点名称：</label>
                          <div class="col-lg-7">
                              <input type="text" class="form-control" name="mdkdmc" [(ngModel)]="s_obj.mdkdmc" />
                          </div>
                      </div>
                  </div>
                  <div class="col-lg-3">
                      <div class="form-group">
                          <label class="col-lg-5 control-label">是否关联电子标签：</label>
                          <div class="col-lg-7">
                              <select class="form-control" name="sjgldzbq" id="sjgldzbqSelect"
                                  [(ngModel)]="s_obj.sjgldzbq" (change)="inittable_sjb()"></select>
                          </div>
                      </div>
                  </div>
                  <div class="col-lg-1">
                      <button type="button" class="btn blue" (click)="inittable_sjb()">
                          <i class="fa fa-search"></i> 搜索
                      </button>
                  </div>
              </form>
          </div>
          <div class="row">
              <div class="col-lg-12" *ngIf="table_sjb">
                  <my-table [o]="table_sjb"></my-table>
              </div>
          </div>
      </div>
  </div>
  <div class="modal fad" id="xq_sjbModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
      <div class="modal-dialog smmodalwidth" role="document">
          <div class="modal-content">
              <div class="modal-header">
                  <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                          aria-hidden="true">&times;</span></button>
                  <h4 class="modal-title" id="myModalLabel">详情</h4>
              </div>
              <div class="modal-body">
                  <div class="row">
                      <div class="col-lg-12" *ngIf="xqobj_sjb">
                          <xq-form [o]="xqobj_sjb"></xq-form>
                      </div>
                  </div>
              </div>
              <div class="modal-footer">
                  <button type="button" class="btn btn-default" data-dismiss="modal">确 定</button>
              </div>
          </div>
      </div>
  </div>
  <div class="modal fad" id="add_sjbModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
      <div class="modal-dialog bigmodalwidth" role="document">
          <div class="modal-content">
              <div class="modal-header">
                  <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                          aria-hidden="true">&times;</span></button>
                  <h4 class="modal-title" id="myModalLabel">{{modaltitle}}</h4>
              </div>
              <div class="modal-body">
                  <div class="row">
                      <form class="form-horizontal" role="form" [formGroup]="formgroup_sjb">
                          <div class="col-lg-12">
                              <div class="form-group">
                                  <label class="control-label col-lg-2">
                                      <require-label></require-label>
                                      试卷包名称：
                                  </label>
                                  <div class="col-lg-8">
                                      <input type="text" class="form-control" formControlName="sjbmcCtr" />
                                      <div
                                          *ngIf="formgroup_sjb.get('sjbmcCtr').invalid&&formgroup_sjb.get('sjbmcCtr').dirty">
                                          <span *ngIf="formgroup_sjb.hasError('myrequire','sjbmcCtr')">
                                              <tool-tip [o]="formgroup_sjb.getError('myrequire','sjbmcCtr').msg">
                                              </tool-tip>
                                          </span>
                                      </div>
                                  </div>
                              </div>
                          </div>
                          <div class="col-lg-12">
                              <div class="form-group">
                                  <label class="control-label col-lg-2">
                                      <require-label></require-label>
                                      目的考区编号：
                                  </label>
                                  <div class="col-lg-8">
                                      <input type="text" class="form-control" formControlName="mdkqbhCtr" />
                                      <div
                                          *ngIf="formgroup_sjb.get('mdkqbhCtr').invalid&&formgroup_sjb.get('mdkqbhCtr').dirty">
                                          <span *ngIf="formgroup_sjb.hasError('myrequire','mdkqbhCtr')">
                                              <tool-tip [o]="formgroup_sjb.getError('myrequire','mdkqbhCtr').msg">
                                              </tool-tip>
                                          </span>
                                      </div>
                                  </div>
                              </div>
                          </div>
                          <div class="col-lg-12">
                              <div class="form-group">
                                  <label class="control-label col-lg-2">
                                      <require-label></require-label>
                                      目的考区名称：
                                  </label>
                                  <div class="col-lg-8">
                                      <input type="text" class="form-control" formControlName="mdkqmcCtr" />
                                      <div
                                          *ngIf="formgroup_sjb.get('mdkqmcCtr').invalid&&formgroup_sjb.get('mdkqmcCtr').dirty">
                                          <span *ngIf="formgroup_sjb.hasError('myrequire','mdkqmcCtr')">
                                              <tool-tip [o]="formgroup_sjb.getError('myrequire','mdkqmcCtr').msg">
                                              </tool-tip>
                                          </span>
                                      </div>
                                  </div>
                              </div>
                          </div>
                          <div class="col-lg-12">
                              <div class="form-group">
                                  <label class="control-label col-lg-2">
                                      <require-label></require-label>
                                      目的考点编号：
                                  </label>
                                  <div class="col-lg-8">
                                      <input type="text" class="form-control" formControlName="mdkdbhCtr" />
                                      <div
                                          *ngIf="formgroup_sjb.get('mdkdbhCtr').invalid&&formgroup_sjb.get('mdkdbhCtr').dirty">
                                          <span *ngIf="formgroup_sjb.hasError('myrequire','mdkdbhCtr')">
                                              <tool-tip [o]="formgroup_sjb.getError('myrequire','mdkdbhCtr').msg">
                                              </tool-tip>
                                          </span>
                                      </div>
                                  </div>
                              </div>
                          </div>
                          <div class="col-lg-12">
                              <div class="form-group">
                                  <label class="control-label col-lg-2">
                                      <require-label></require-label>
                                      目的考点名称：
                                  </label>
                                  <div class="col-lg-8">
                                      <input type="text" class="form-control" formControlName="mdkdmcCtr" />
                                      <div
                                          *ngIf="formgroup_sjb.get('mdkdmcCtr').invalid&&formgroup_sjb.get('mdkdmcCtr').dirty">
                                          <span *ngIf="formgroup_sjb.hasError('myrequire','mdkdmcCtr')">
                                              <tool-tip [o]="formgroup_sjb.getError('myrequire','mdkdmcCtr').msg">
                                              </tool-tip>
                                          </span>
                                      </div>
                                  </div>
                              </div>
                          </div>
                          <div class="col-lg-12">
                              <label class="control-label col-lg-2">
                                  <require-label></require-label>
                                  试卷信息列表：
                              </label>
                              <div class="col-lg-10">
                                  <table class="table table-bordered">
                                      <thead>
                                          <tr>
                                              <th>
                                                  <require-label></require-label>
                                                  科目
                                              </th>
                                              <th>
                                                  <require-label></require-label>
                                                  正卷试卷袋数量
                                              </th>
                                              <th>
                                                  <require-label></require-label>
                                                  正卷答题卡袋数量
                                              </th>
                                              <th>
                                                  <require-label></require-label>
                                                  备用卷袋数量
                                              </th>
                                              <th>
                                                  <require-label></require-label>
                                                  备用卷答题卡数量
                                              </th>
                                              <th width="8%">操作</th>
                                          </tr>
                                      </thead>
                                      <tbody id="sjxxRoom">
                                          <tr *ngFor="let item of sjxxList;index as index">
                                              <td>
                                                  <select class="form-control" name="km" [value]="item.kmm">
                                                      <option *ngFor="let kmobj of kmList" [value]="kmobj.kmm">
                                                          {{kmobj.kmmc}}</option>
                                                  </select>
                                              </td>
                                              <td>
                                                  <input type="text" class="form-control" name="zjsjdsl"
                                                      [value]="item.zjsjdsl" />
                                                  <span class="text-danger help-block"
                                                      *ngIf="item.zjsjdslwarnflag">{{item.zjsjdslwarntext}}</span>
                                              </td>
                                              <td>
                                                  <input type="text" class="form-control" name="zjdtkdsl"
                                                      [value]="item.zjdtkdsl" />
                                                  <span class="text-danger help-block"
                                                      *ngIf="item.zjdtkdslwarnflag">{{item.zjdtkdslwarntext}}</span>
                                              </td>
                                              <td>
                                                  <input type="text" class="form-control" name="byjdsl"
                                                      [value]="item.byjdsl" />
                                                  <span class="text-danger help-block"
                                                      *ngIf="item.byjdslwarnflag">{{item.byjdslwarntext}}</span>
                                              </td>
                                              <td>
                                                  <input type="text" class="form-control" name="byjdtkdsl"
                                                      [value]="item.byjdtkdsl" />
                                                  <span class="text-danger help-block"
                                                      *ngIf="item.byjdtkdslwarnflag">{{item.byjdtkdslwarntext}}</span>
                                              </td>
                                              <td width="8%">
                                                  <ng-container>
                                                      <button type="button" class="btn btn-xs red"
                                                          (click)="actionTemplate(0,index)">
                                                          <i class="fa fa-trash"></i> 删除
                                                      </button>
                                                  </ng-container>
                                              </td>
                                          </tr>
                                          <div class="addbtnroom">
                                              <button type="button" class="btn btn-xs blue"
                                                  (click)="actionTemplate(1,'')">
                                                  <i class="fa fa-plus"></i> 添加
                                              </button>
                                          </div>
                                      </tbody>
                                  </table>
                              </div>
                          </div>
                      </form>
                  </div>
              </div>
              <div class="modal-footer">
                  <button type="button" class="btn btn-default" data-dismiss="modal">取 消</button>
                  <button type="button" class="btn btn-primary" (click)="sure_sjb()">确 定</button>
              </div>
          </div>
      </div>
  </div>
</div>
  `,
  styles: [
    "td{position: relative;}",
    "table tbody{display: block;height: 250px;overflow-y: scroll}",
    "table thead,table tr{display: table;width: 100%;table-layout: fixed}",
    "table thead{width: calc(100% - 1.2em)}",
    ".addbtnroom{width: 100%;height: 30px;padding-left: 20px;padding-top: 10px;margin-bottom: 15px}"
  ]
})
export class SjbxxglComponent implements OnInit, OnDestroy {
  sjb_btns2: any[] = [{ name: "sjb_add" }, { name: "sjb_refresh" }];
  pagetitleobj: any;
  userinfo: any;
  actionobj1: any[] = [];
  sjb_btns: any[] = ["sjb_add", "sjb_refresh"];
  sjb_c_btns: any[] = ["remove_sjb", "edite_sjb", "xq_sjb"];
  hascbtns: any[] = [];
  s_obj: any = {
    sjbmc: "",
    ksjhbh: "",
    mdkqmc: "",
    mdkdmc: "",
    sjgldzbq: "", // 	是否关联电子标签：0 已关联；1 未关联。
  };
  table_sjb: any;
  caozuo_sjbbh: string;
  xqobj_sjb: any;
  formgroup_sjb: FormGroup;
  modalflag: string = "";
  modaltitle: string = "";
  sjxxList: any[] = [];
  editesjxxList: any[] = [];// 编辑试卷包的试卷信息原始
  kmList: any[] = [];
  noReactiveField: any = {
    ksjhbh: ""
  }

  /**
   * 初始化按钮
   */
  initactions(): void {
    // this.commservice.getroleBtns(this.sjb_btns, this.sjb_c_btns, this.userinfo.nowroleid, "tablesjb", (hasportbtns, hastablebtns) => {
    //   this.actionobj1 = hasportbtns;
    //   this.hascbtns = hastablebtns;
    // });
    this.commservice.getroleBtns_not_permissions(this.sjb_btns, this.sjb_c_btns, "tablesjb", (hasportbtns, hastablebtns) => {
      this.actionobj1 = hasportbtns;
      this.hascbtns = hastablebtns;
    });

  }

  /**
   * 按钮的处理回调
   * @param e
   */
  actionHander(e: any): void {
    console.log(11)
    const fromv = e.pfrom;
    const actiontype = e.type;
    if (fromv === "tablesjb") {
      if (actiontype === "refresh") {
        // 刷新
        this.table_sjb = Object.assign({}, this.table_sjb);
      } else if (actiontype === "add") {
        // 添加
        this.add_sjb();
      }
    }
  }

  /**
   * 添加试卷包
   */
  add_sjb(): void {
    this.modalflag = "add";
    this.modaltitle = "添加试卷包信息";
    this.formgroup_sjb.reset();
    this.sjxxList = [];
    $("#add_sjbModal").modal({
      keyboard: true,
      backdrop: "static"
    });
  }

  inittable_sjb(): void {
    const that = this;
    this.table_sjb = {
      tableid: "table_sjb",
      url: "sjgz/jcsj/sjbxxlb",
      reqobj: this.s_obj,
      sidePagination: "server",
      pageSize: 10,
      pageList: [10, 20, 30],
      total: "totalRows",
      rows: "sjbxxList",
      columns: [{
        checkbox: true,
        visible: false
      }, {
        title: '序号',
        field: "ordernum",
        cellStyle: function () {
          return {
            classes: 'td1'
          }
        }
      }, {
        title: "试卷包名称",
        field: "sjbmc",
        cellStyle: function () {
          return {
            classes: 'td2'
          }
        }
      }, {
        title: "目的地考点名称",
        field: "mdkdmc",
        cellStyle: function () {
          return {
            classes: 'td3'
          }
        }
      }, {
        title: "目的地考区名称",
        field: "mdkqmc",
        cellStyle: function () {
          return {
            classes: 'td4'
          }
        }
      }, {
        title: "试卷袋数量",
        field: "sjdsl",
        cellStyle: function () {
          return {
            classes: 'td1'
          }
        }
      }, {
        title: "操作",
        field: "sjbbh",
        events: this.myevents,
        formatter: function (v) {
          let str = "";
          if (that.hascbtns.indexOf("edite_sjb") != -1) {
            str += "<button type='button' class='btn btn-xs btn-primary edite_sjb'><i class='fa fa-pencil'></i> 编辑</button>";
          }
          if (that.hascbtns.indexOf("remove_sjb") != -1) {
            str += "<button type='button' class='btn btn-xs btn-warning remove_sjb'><i class='fa fa-trash'></i> 删除</button>";
          }
          if (that.hascbtns.indexOf("xq_sjb") != -1) {
            str += "<button type='button' class='btn btn-xs btn-info xq_sjb'><i class='fa fa-search'></i> 详情</button>";
          }
          return str;
        }
      }]
    };
    //   this.table_sjb.pageSize = 50;
    //   this.table_sjb.pageList = [50];
    //   this.table_sjb.hiddenPageinfoLink = true;
    //   this.table_sjb.paginationLoop = true;
    //   this.table_sjb.paginationPreText = "<i class='fa fa-arrow-left'></i>上一页";
    //   this.table_sjb.paginationNextText = "下一页<i class='fa fa-arrow-right'></i>";
    //   this.table_sjb.height = this.commservice.getViewPort().height - this.commservice.topH - this.commservice.topmenuH - 240 - 50;
    this.table_sjb.height = this.commservice.getViewPort().height - this.commservice.topH - this.commservice.topmenuH - 240 - 50;
  }


  @HostListener("window:myevents", ["$event"])
  myevents = {
    "click .edite_sjb": function (e, value, row, index) {
      // 编辑
      this.caozuo_sjbbh = value;
      this.edite_sjb();
    }.bind(this),
    "click .remove_sjb": function (e, value, row, index) {
      // 单条删除
      this.caozuo_sjbbh = value;
      if (!this.caozuo_sjbbh) {
        return
      }
      this.commservice.myswal("warning", "确定删除？", "", "cz", () => {
        this.removeDone_sjb();
      });
    }.bind(this),
    "click .xq_sjb": function (e, value, row, index) {
      // 详情
      this.caozuo_sjbbh = value;
      this.xq_sjb();
    }.bind(this),
  }

  /**
   * 单条删除试卷包信息
   */
  removeDone_sjb(): void {
    const url = "sjgz/jcsj/sjbxxsc";
    this.itservice.kwPtPost(url, {
      sjbbhs: [this.caozuo_sjbbh]
    }).subscribe(resp => {
      this.commservice.handResponse("put", url, resp, (arg1, resp, arg2) => {
        if (arg1) {
          if (arg2) {
            this.commservice.myswal("success", resp, "", "ts", null);
            this.table_sjb = Object.assign({}, this.table_sjb);
          } else {
            this.commservice.myswal("warning", resp, "", "ts", null);
          }
        } else {
          this.removeDone_sjb();
        }
      });
    })
  }

  /**
   * 试卷包详情
   */
  xq_sjb(): void {
    if (!this.caozuo_sjbbh) {
      return
    }
    const url = "sjgz/jcsj/sjbxxxq";
    this.itservice.kwPtPost(url, {
      sjbbh: this.caozuo_sjbbh
    }).subscribe(resp => {
      this.commservice.handResponse("get", url, resp, (arg1, resp, arg2) => {
        if (arg1) {
          if (arg2) {
            this.xqobj_sjb = {
              from: "sjbxx",
              keyValue: resp
            };
            $("#xq_sjbModal").modal({
              keyboard: true,
              backdrop: "static"
            });
          } else {
            this.commservice.myswal("warning", resp, "", "ts", null);
          }
        } else {
          this.xq_sjb();
        }
      })
    })
  }

  /**
   * 编辑试卷包信息
   */
  edite_sjb(): void {
    if (!this.caozuo_sjbbh) {
      return
    }
    this.modalflag = "edite";
    this.modaltitle = "编辑试卷包信息";
    const url = "sjgz/jcsj/sjbxxxq";
    this.itservice.kwPtPost(url, {
      sjbbh: this.caozuo_sjbbh
    }).subscribe(resp => {
      this.commservice.handResponse("get", url, resp, (arg1, resp, arg2) => {
        if (arg1) {
          if (arg2) {
            const sjbmc = resp.sjbmc;
            const mdkqbh = resp.mdkqbh;
            const mdkqmc = resp.mdkqmc;
            const mdkdbh = resp.mdkdbh;
            const mdkdmc = resp.mdkdmc;
            this.formgroup_sjb.patchValue({
              sjbmcCtr: sjbmc,
              mdkqbhCtr: mdkqbh,
              mdkqmcCtr: mdkqmc,
              mdkdbhCtr: mdkdbh,
              mdkdmcCtr: mdkdmc
            });
            const sjxxList = resp.sjxxList;
            if (sjxxList && sjxxList.length) {
              this.editesjxxList = sjxxList;
              this.sjxxList = sjxxList.map((item, index) => {
                return {
                  sjxxbh: item.sjxxbh, // 试卷信息编号
                  kmm: item.kmm,
                  kmmc: item.kmmc,
                  zjsjdsl: item.zjsjdsl,
                  zjsjdslwarnflag: false,
                  zjsjdslwarntext: "",
                  zjdtkdsl: item.zjdtkdsl,
                  zjdtkdslwarnflag: false,
                  zjdtkdslwarntext: "",
                  byjdsl: item.byjdsl,
                  byjdslwarnflag: false,
                  byjdslwarntext: "",
                  byjdtkdsl: item.byjdtkdsl,
                  byjdtkdslwarnflag: false,
                  byjdtkdslwarntext: ""
                }
              });
            }
            $("#add_sjbModal").modal({
              keyboard: true,
              backdrop: "static"
            });
          } else {
            this.commservice.myswal("warning", resp, "", "ts", null);
          }
        } else {
          this.xq_sjb();
        }
      })
    })
  }

  /**
   * 创建试卷包表单
   */
  createformgroup_sjb(): void {
    this.formgroup_sjb = this.fb.group({
      sjbmcCtr: ["", [this.myValidator.myrequired('试卷包名称')]],
      mdkqbhCtr: ["", [this.myValidator.myrequired("目的考区编号")]],
      mdkqmcCtr: ["", [this.myValidator.myrequired("目的考区名称")]],
      mdkdbhCtr: ["", [this.myValidator.myrequired("目的考点编号")]],
      mdkdmcCtr: ["", [this.myValidator.myrequired("目的考点名称")]],
    });
    this.getkm();
  }

  getkm(): void {
    const url = "ksyw/ksjhgl/ksjhbhcxkmlb";
    this.itservice.kwPtPost(url, {
      ksjhbh: this.noReactiveField.ksjhbh
    }).subscribe(resp => {
      this.commservice.handResponse("get", url, resp, (arg1, resp, arg2) => {
        if (arg1) {
          if (arg2) {
            const ksjhbhCxkmList = resp.ksjhbhCxkmList;
            if (ksjhbhCxkmList && ksjhbhCxkmList.length) {
              this.kmList = ksjhbhCxkmList;
            }
          } else {
            this.commservice.myswal("warning", resp, "", "ts", null);
          }
        } else {
          this.getkm();
        }
      });
    });
  }

  /**
   * 验证+ 保存
   */
  sure_sjb(): void {
    if (this.formgroup_sjb.invalid) {
      const ctrols = this.formgroup_sjb.controls;
      for (let path in ctrols) {
        ctrols[path].markAsDirty();
        ctrols[path].updateValueAndValidity();
      }
      return
    }
    // 验证，试卷模板信息
    let yzindex_valid: number = 1;
    if (!this.sjxxList.length) {
      yzindex_valid *= 0;
      this.commservice.myswal("warning", "缺少试卷信息", "", "ts", null);
      return
    }
    this.sjxxList.forEach((item, index) => {
      const kmmv = $("#sjxxRoom select[name='km']").eq(index).val(); // 科目码
      const kmmcv = $("#sjxxRoom select[name='km']").eq(index).find("option:selected").text(); // 科目名称
      const zjsjdslv = $("#sjxxRoom input[name='zjsjdsl']").eq(index).val(); // 正卷试卷袋数量
      if (!this.myValidator.isZrs(zjsjdslv)) {
        item.zjsjdslwarnflag = true;
        item.zjsjdslwarntext = "不合法的数据";
        yzindex_valid *= 0;
      } else {
        item.zjsjdslwarnflag = false;
        item.zjsjdslwarntext = "";
        yzindex_valid *= 1;
      }
      const zjdtkdslv = $("#sjxxRoom input[name='zjdtkdsl']").eq(index).val(); // 正卷答题卡袋数量
      if (!this.myValidator.isZrs(zjdtkdslv)) {
        item.zjdtkdslwarnflag = true;
        item.zjdtkdslwarntext = "不合法的数据";
        yzindex_valid *= 0;
      } else {
        item.zjdtkdslwarnflag = false;
        item.zjdtkdslwarntext = "";
        yzindex_valid *= 1;
      }
      const byjdslv = $("#sjxxRoom input[name='byjdsl']").eq(index).val(); // 备用卷袋数量
      if (!this.myValidator.isZrs(byjdslv)) {
        item.byjdslwarnflag = true;
        item.byjdslwarntext = "不合法的数据";
        yzindex_valid *= 0;
      } else {
        item.byjdslwarnflag = false;
        item.byjdslwarntext = "";
        yzindex_valid *= 1;
      }
      const byjdtkdslv = $("#sjxxRoom input[name='byjdtkdsl']").eq(index).val(); // 备用卷答题卡数量
      if (!this.myValidator.isZrs(byjdtkdslv)) {
        item.byjdtkdslwarnflag = true;
        item.byjdtkdslwarntext = "不合法的数据";
        yzindex_valid *= 0;
      } else {
        item.byjdtkdslwarnflag = false;
        item.byjdtkdslwarntext = "";
        yzindex_valid *= 1;
      }
    });
    if (!yzindex_valid) {
      this.commservice.myswal("warning", "试卷信息中存在不合法数据", "", "ts", null);
      return
    }
    const flag = this.modalflag;
    const formgroupv = this.formgroup_sjb.getRawValue();
    const sjbmcv = this.commservice.removeSpace(true, formgroupv.sjbmcCtr);
    const mdkqbhv = this.commservice.removeSpace(true, formgroupv.mdkqbhCtr);
    const mdkqmcv = this.commservice.removeSpace(true, formgroupv.mdkqmcCtr);
    const mdkdbhv = this.commservice.removeSpace(true, formgroupv.mdkdbhCtr);
    const mdkdmcv = this.commservice.removeSpace(true, formgroupv.mdkdmcCtr);
    const sjxxListv: any[] = [];
    this.sjxxList.forEach((item, index) => {
      const sjxxbhv = item.sjxxbh;
      const kmmv = $("#sjxxRoom select[name='km']").eq(index).val(); // 科目码
      const kmmcv = $("#sjxxRoom select[name='km']").eq(index).find("option:selected").text(); // 科目名称
      const zjsjdslv = $("#sjxxRoom input[name='zjsjdsl']").eq(index).val(); // 正卷试卷袋数量
      const zjdtkdslv = $("#sjxxRoom input[name='zjdtkdsl']").eq(index).val(); // 正卷答题卡袋数量
      const byjdslv = $("#sjxxRoom input[name='byjdsl']").eq(index).val(); // 备用卷袋数量
      const byjdtkdslv = $("#sjxxRoom input[name='byjdtkdsl']").eq(index).val(); // 备用卷答题卡数量
      const obj = {
        kmm: kmmv,
        kmmc: kmmcv,
        zjsjdsl: zjsjdslv,
        zjdtkdsl: zjdtkdslv,
        byjdsl: byjdslv,
        byjdtkdsl: byjdtkdslv
      }
      if (flag == "edite") {
        obj["sjxxbh"] = sjxxbhv;
      }
      sjxxListv.push(obj);
    });

    this.commservice.blockUI({
      boxed: true,
      message: "保存中...",
      zIndex: 10051,
      overlayColor: "#67809F"
    });

    if (flag == "add") {
      const url = "sjgz/jcsj/sjbxxxz";
      this.itservice.kwPtPost(url, {
        ksjhbh: this.noReactiveField.ksjhbh,
        sjbmc: sjbmcv,
        mdkqbh: mdkqbhv,
        mdkqmc: mdkqmcv,
        mdkdbh: mdkdbhv,
        mdkdmc: mdkdmcv,
        sjxxList: sjxxListv
      }).subscribe(resp => {
        this.commservice.handResponse("put", url, resp, (arg1, resp, arg2) => {
          if (arg1) {
            if (arg2) {
              this.commservice.myswal("success", resp, "", "ts", null);
              this.table_sjb = Object.assign({}, this.table_sjb);
              $("#add_sjbModal").modal("hide");
            } else {
              this.commservice.myswal("warning", resp, "", "ts", null);
            }
          } else {
            this.add_sjb();
          }
        });
      })
    } else if (flag == "edite") {
      const nowsjxxList = sjxxListv; // 要保存的
      const nowsjxxBhlist: any[] = [];
      const sjbxgXzsjxxListv: any[] = [] // 新增加的
      const sjbxgScsjxxbhsv: any[] = []; // 删除的
      const sjbxgXgsjxxListv: any[] = []; //修改的
      nowsjxxList.forEach((item) => {
        const sjxxbh = item.sjxxbh;  // 试卷信息编号
        nowsjxxBhlist.push(sjxxbh);
        if (!sjxxbh) {
          // 新增加的
          sjbxgXzsjxxListv.push({
            kmm: item.kmm,
            kmmc: item.kmmc,
            zjsjdsl: item.zjsjdsl,
            zjdtkdsl: item.zjdtkdsl,
            byjdsl: item.byjdsl,
            byjdtkdsl: item.byjdtkdsl
          });
        } else {
          // 修改的
          sjbxgXgsjxxListv.push(item)
        }
      });
      // 删除的编号
      this.editesjxxList.forEach((item) => {
        const sjxxbh = item.sjxxbh;
        if (nowsjxxBhlist.indexOf(sjxxbh) == -1) {
          sjbxgScsjxxbhsv.push(sjxxbh)
        }
      });
      const url = "sjgz/jcsj/sjbxxxg";
      this.itservice.kwPtPost(url, {
        sjbbh: this.caozuo_sjbbh,
        ksjhbh: this.noReactiveField.ksjhbh,
        sjbmc: sjbmcv,
        mdkqbh: mdkqbhv,
        mdkqmc: mdkqmcv,
        mdkdbh: mdkdbhv,
        mdkdmc: mdkdmcv,
        sjbxgXgsjxxList: sjbxgXgsjxxListv,
        sjbxgXzsjxxList: sjbxgXzsjxxListv,
        sjbxgScsjxxbhs: sjbxgScsjxxbhsv
      }).subscribe(resp => {
        this.commservice.handResponse("put", url, resp, (arg1, resp, arg2) => {
          if (arg1) {
            if (arg2) {
              this.commservice.myswal("success", resp, "", "ts", null);
              this.table_sjb = Object.assign({}, this.table_sjb);
              $("#add_sjbModal").modal("hide");
            } else {
              this.commservice.myswal("warning", resp, "", "ts", null);
            }
          } else {
            this.add_sjb();
          }
        });
      });
    }


  }


  /**
   *  试卷信息的控制
   * @param {number} flag
   * 0----删除一个试卷信息模板
   * 1----添加一个试卷信息模板
   */
  actionTemplate(flag: number, index: any): void {
    switch (flag) {
      case 1:
        this.sjxxList.push({
          sjxxbh: "", // 试卷信息编号
          kmm: this.kmList[0].kmm,
          kmmc: "",
          zjsjdsl: "",
          zjsjdslwarnflag: false,
          zjsjdslwarntext: "",
          zjdtkdsl: "",
          zjdtkdslwarnflag: false,
          zjdtkdslwarntext: "",
          byjdsl: "",
          byjdslwarnflag: false,
          byjdslwarntext: "",
          byjdtkdsl: "",
          byjdtkdslwarnflag: false,
          byjdtkdslwarntext: ""
        });
        break;
      case 0:
        // 无条件 删除 该条模板
        this.sjxxList.splice(index, 1);
        break
    }
  }

  /**
   * 试卷包搜索表单的处理
   */
  createsearchForm(): void {
    const sjgldzbqData = [{
      code: "0",
      name: "已关联"
    }, {
      code: "1",
      name: "未关联"
    }];
    $("#sjgldzbqSelect").empty();
    let str: string = "<option value=''>--请选择--</option>";
    sjgldzbqData.forEach((item) => {
      str += "<option value='" + item.code + "'>" + item.name + "</option>";
    });
    $("#sjgldzbqSelect").append(str);
    $("#sjgldzbqSelect").selectpicker({
      size: 5
    });
    this.s_obj.sjgldzbq = "";
  }

  /**
   * 获取用户信息
   */
  getuserinfo(): void {
    this.commservice.getUserinfo((info) => {
      this.userinfo = info;
    });
  }


  constructor(private localstorage: LocalStorageService,
    private commservice: CommService,
    private itservice: MyhttpService,
    private fb: FormBuilder,
    private myValidator: MyValidator) {


  }

  ngOnInit() {

    this.initactions();
    const e = {
      pfrom: 'tablesjb',
      type: 'refresh'
    }
    this.actionHander(e)
    this.pagetitleobj = sjgzxinModuledatas.sjbxx;
    this.s_obj.ksjhbh = this.localstorage.get("hksjh");
    this.noReactiveField.ksjhbh = this.localstorage.get("hksjh");
    this.getuserinfo();
    this.createsearchForm();
    this.inittable_sjb();
    this.createformgroup_sjb();

  }

  ngOnDestroy() {

  }

}
